package com.yunkeji.api.auth.controller;

import com.yunkeji.api.auth.bo.ChangeTokenBo;
import com.yunkeji.api.auth.bo.SendCodeBo;
import com.yunkeji.api.auth.bo.ToLoginBo;
import com.yunkeji.api.auth.bo.ToRegBo;
import com.yunkeji.api.auth.service.AuthService;
import com.yunkeji.api.auth.vo.ToLoginVo;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.enums.app.AccountTypeEnums;
import com.yunkeji.config.annotation.ApiAuth;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;
/** @author 老张 */
/** 授权相关接口 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/auth")
public class AuthController extends BaseController {
    private final AuthService authService;

    /** 登录-性别返回2=未知,需要强制更新性别,调用更新用户信息 */
    @PostMapping("/toLogin")
    @RepeatSubmit(interval = 3, timeUnit = TimeUnit.SECONDS, message = "操作过快,三秒后再试")
    public R<ToLoginVo> toLogin(@RequestBody @Valid ToLoginBo bo) {
        return R.ok(authService.toLogin(bo));
    }

    /** 登录失效后置换新token */
    @PostMapping("/changeToken")
    @ApiAuth(allow = {AccountTypeEnums.客服, AccountTypeEnums.用户})
    public R<ToLoginVo> changeToken(@RequestBody @Valid ChangeTokenBo bo) {
        return R.ok(authService.changeToken(bo));
    }

    /** 发送验证码 */
    @PostMapping("/sendCode")
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.MINUTES, message = "发送频率过快")
    public R sendCode(@RequestBody @Valid SendCodeBo bo) {
        authService.sendCode(bo);
        return R.ok("发送成功");
    }

    /** 账号注册 */
    @PostMapping("/register")
    public R<ToLoginVo> register(@RequestBody @Valid ToRegBo bo) {
        ToLoginVo vo = authService.registerByCode(bo);
        return R.ok(vo);
    }

    /** 重置密码 */
    @PostMapping("/resetPwd")
    public R resetPwd(@RequestBody @Valid ToRegBo bo) {
        authService.resetPwd(bo);
        return R.ok();
    }
}
